package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Category;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //1、购物车是否存在，购物车信息是一条条添加的，根据用户id和商品/套餐id查询是否已存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //1.1、查询当前用户id，加入实体类
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //1.2 根据用户id和商品/套餐id查询是否已存在
        ShoppingCart cartInfo =  this.shoppingCartMapper.getInfo(shoppingCart);
        if(!ObjectUtils.isEmpty(cartInfo)){
            //2、存在则商品数量+1
            cartInfo.setNumber(cartInfo.getNumber() + 1);
            //2.1、根据用户id和商品/套餐id修改数据
            this.shoppingCartMapper.updateInfo(cartInfo);
        }else {
            //3.1、不存在则需要新建购物车商品信息
            //3.2、查询菜品/套餐基本信息，组装购物车实体类
            if(shoppingCartDTO.getDishId() != null){
                DishVO dish = this.dishMapper.getById(shoppingCartDTO.getDishId());
                shoppingCart.setNumber( 1 );
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setName(dish.getName());
            }else {
                SetmealVO setmeal = this.setmealMapper.getByID(shoppingCartDTO.getSetmealId());
                shoppingCart.setNumber( 1 );
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setName(setmeal.getName());
            }
            //3.3、新增购物车
            this.shoppingCartMapper.add(shoppingCart);
        }

    }

    /**
     * 查询购物车
     */
    @Override
    public List<ShoppingCart> getByUserId() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return this.shoppingCartMapper.selectList(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shoppingCartMapper.delete(BaseContext.getCurrentId());
    }

    /**
     * 减少购物车商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void updateShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //1、查询购物车商品是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //1.1、查询当前用户id，加入实体类
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //1.2 根据用户id和商品/套餐id查询是否已存在
        ShoppingCart cartInfo =  this.shoppingCartMapper.getInfo(shoppingCart);
        if(ObjectUtils.isEmpty(cartInfo)){
            throw new RuntimeException("脑子有坑才会出现这种情况");
        }else if(cartInfo.getNumber() > 1){
            //2、数量 < 1 (不包括1) 数量减一，修改购物车
            cartInfo.setNumber(cartInfo.getNumber() - 1);
            //2.1、根据用户id和商品/套餐id修改数据
            this.shoppingCartMapper.updateInfo(cartInfo);
        }else {
            //3、否则删除购物车记录
            this.shoppingCartMapper.deleteByDishSetmealID(cartInfo.getId());
        }

    }
}
